package bal.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class CadastrarJogadorActivity extends Activity {
	
	Button voltar;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cadastrar_jogador);

		Spinner s = (Spinner) findViewById(R.id.posicaoJogador);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
		        this, R.array.posicoes, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s.setAdapter(adapter);
		
		Button cadastrarJogador = (Button) findViewById(R.id.buttonCadastrarJogador);
		cadastrarJogador.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				cadastrarJogador();
			}
		});
		
		voltar = (Button) findViewById(R.id.buttonVoltar);
		
		voltar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				menuPrincipal();
			}
		});
	}
	
	private void cadastrarJogador() {
		String nome = ((EditText) findViewById(R.id.nome)).getText().toString();
		String peso = ((EditText) findViewById(R.id.peso)).getText().toString();
		String altura = ((EditText) findViewById(R.id.altura)).getText().toString();
		String posicao = getPosicao();

		String[] keys = { "nome", "peso", "altura", "posicao" };
		String[] values = { nome, peso, altura, posicao };

		String response = ConexaoServidor.execute("CadastroJogadorServlet", keys, values);
		
		if (response.equals("sucesso")) {
			Intent intent = new Intent(this, HomeActivity.class);
			startActivity(intent);
			this.finish();
		} else {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setCancelable(false)
					.setNeutralButton("OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});

			if (response.equals("erro_banco")) {
				builder.setMessage("Um erro ocorreu durante o acesso ao servidor.");
			} else {
				builder.setMessage(response);
			}

	        AlertDialog alert = builder.create();
	        alert.show();
		}
	}

	private String getPosicao() {
		Spinner spinnerPosicao = (Spinner) findViewById(R.id.posicaoJogador);
		String posicao = spinnerPosicao.getSelectedItem().toString();
		if (posicao.equals("Zagueiro")) {
			return "0";
		} else if (posicao.equals("Lateral")) {
			return "1";
		} else if (posicao.equals("Volante")) {
			return "2";
		} else if (posicao.equals("Meia")) {
			return "3";
		} else if (posicao.equals("Atacante")) {
			return "5";
		} else {
			return null;
		}
	}
	
	public void menuPrincipal() {
		Intent intent = new Intent(this, HomeActivity.class);
		startActivity(intent);
		this.finish();
	}
}
